package com.webchat.admin.controller.mall;


import com.webchat.admin.service.mall.MallCategoryService;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.config.annotation.ValidateLogin;
import com.webchat.common.config.annotation.ValidatePermission;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.vo.request.mall.SaveMallCategoryRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin-service/mall/category")
public class MallCategoryController {

    @Autowired
    private MallCategoryService mallCategoryService;


    /**
     * 创建或者更新商品分类
     *
     * @param requestVO
     * @return
     */
    @ValidatePermission
    @PostMapping("/save")
    public APIResponseBean<Long> save(@RequestBody SaveMallCategoryRequestVO requestVO) {

        String userId = SessionHelper.getCurrentUserId();
        requestVO.setOperator(userId);
        Long id = mallCategoryService.saveCategory(requestVO);
        return APIResponseBeanUtil.success(id);
    }

    /**
     * 发布商品分类
     *
     * @param id
     * @return
     */
    @ValidatePermission
    @PostMapping("/publish/{id}")
    APIResponseBean<Long> publish(@PathVariable Long id) {

        String userId = SessionHelper.getCurrentUserId();
        boolean res = mallCategoryService.publish(id, userId);
        return APIResponseBeanUtil.success(res);
    }

    /**
     * 撤回
     *
     * @param id
     * @return
     */
    @ValidatePermission
    @PostMapping("/invoke/{id}")
    APIResponseBean<Long> invoke(@PathVariable Long id) {

        String userId = SessionHelper.getCurrentUserId();
        boolean res = mallCategoryService.invoke(id, userId);
        return APIResponseBeanUtil.success(res);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ValidatePermission
    @PostMapping("/delete/{id}/{userId}")
    APIResponseBean<Long> delete(@PathVariable Long id) {

        String userId = SessionHelper.getCurrentUserId();
        boolean res = mallCategoryService.delete(id, userId);
        return APIResponseBeanUtil.success(res);
    }

    /**
     * 修改排序
     *
     * @param id
     * @return
     */
    @ValidatePermission
    @PostMapping("/updateRank/{id}/{rank}")
    APIResponseBean<Long> updateRank(@PathVariable Long id,
                                     @PathVariable Integer rank) {

        String userId = SessionHelper.getCurrentUserId();
        boolean res = mallCategoryService.updateRank(id, rank, userId);
        return APIResponseBeanUtil.success(res);
    }

}
